<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
 <head>
  <title>添加知识点</title>
  <t:base type="jquery,easyui,tools,DatePicker"></t:base>
 </head>
 
 <body>
	<input type="hidden" value="${obj.id }" name="id" id="id">
	<input type="hidden" value="${obj.itemName }" name="itemName" id="itemName">
	
	<div style="padding-top:5px;height: 300px;">
		知识点名称：
		<input type="text" id="input" value=''/>
		<a class='myBtn roundBorder' onclick="addRow()">添加</a>
		<table class="commonTable" id='dataTable'>
			<thead>
				<tr>
					<th>知识点名称</th>
					<th>所属项目</th>
					<th>操作</th>
				</tr>
			</thead> 
			<tbody>
	 			<c:forEach items="${rows}" var="row">
	 				<tr id="row_${row.id}" name="row">
		 				<td>${row.name }</td>
		 				<td>${row.teachItem.itemName }</td>
		 				<td>
							<a href='javascript:void(0)' onclick='deleteRow(this)'>删除</a>
						</td>
 					</tr>
	 			</c:forEach>
			</tbody>
		</table> 
	</div>
	<div style="text-align: center;">
		<button onclick="save()">保存</button>
	</div>
 </body>  	
</div>
 <script>
 	var delIds = [];
 	var addNames = [];
 	var namesInTable = [];
 	$(function(){
 		initParams();
 	});
 	
 	function initParams(){
 		var rows = $("[name='row']");
		for(var i=0;i<rows.length;i++){
			var text = $(rows[i]).children(':first').text();
			namesInTable.push(text);
		}
 	}
 	
 	
 	function addRow(){
 		var input = $("#input").val();
 		if(input==''){
 			tip('请输入知识点名称');
 			return;
 		}
 		
 		if(namesInTable.indexOf(input)!=-1){
 			tip('已存在，请勿重复添加');
 			return;
 		}
 		
 		var html = '';
		html += 
			'<tr>'+
				'<td>'+ input +'</td>'+
				'<td>'+ $("#itemName").val() +'</td>'+
				'<td>'+
					'<a href="javascript:void(0)" onclick="deleteRow(this)">删除</a>'+
				'</td>'+
			'</tr>';
		$('#dataTable').append(html);
		
		addNames.push(input);
 	}
 	
	function deleteRow(obj){
		var tr = $($(obj).parent()).parent();
		var id = $(tr).attr('id') + '';
		if(id!=''&&id!=null&&id!='undefined'){
			delIds.push(id.split('_')[1]+'');
		}
		
		var text = $(tr).children(':first').text();
		var index = addNames.indexOf(text);
		if(index!=-1){
			addNames.splice(index, 1);
		}
		$(tr).remove();
	}
	
	 function save(){
		 var url = 'labProjectController.do?doKnowledgeAdd';
		 $.ajax({
			 url: url,
			 type: 'post',
			 dataType: 'json',
			 data: {'names':addNames.toString(), 'delIds':delIds.toString(), 'id':$("#id").val()},
			 success: function(data){
				 if(data.success){
					 tip(data.msg);
					 location.reload();
				 }else{
					 tip(data.msg);
				 }
			 }
		 });
	 }
	 
 </script>
